package com.group.entity;

import com.group.utils.TimeToken;
import sun.reflect.generics.tree.Tree;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import java.util.Iterator;
import java.util.TreeMap;

/**
 * @author zhanglianyong
 * 2022/12/23 10:46
 */
public class Solution {

    private TreeMap<Integer, SRT> srtMap;

    private String path;

    public Solution(String path) {
        this.path = path;
    }

    /**
     * 解析SRT字幕文件
     *
     * @param srtPath
     * 字幕路径
     */
    public void parseSrt(String srtPath){
        FileInputStream inputStream;
        try {
            inputStream = new FileInputStream(srtPath);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
            return;
        }
        BufferedReader br = new BufferedReader(new InputStreamReader(
                inputStream));
        String line;
        srtMap = new TreeMap<>();
        StringBuilder sb = new StringBuilder();
        int key = 0;
        try {
            while ((line = br.readLine()) != null || sb.length() > 0) {
                if (!"".equals(line) && line != null) {
                    sb.append(line).append("@");
                    continue;
                }
                String[] parseStrs = sb.toString().split("@");
                // 该if为了适应一开始就有空行以及其他不符格式的空行情况
                if (parseStrs.length < 3) {
                    continue;
                }
                SRT srt = new SRT();
                StringBuilder srtBody = new StringBuilder();
                TimeToken timeToken = new TimeToken(srt);
                // 可能1句字幕，也可能2句及以上。
                srt.setId(Integer.valueOf(parseStrs[0].trim()));
                timeToken.parseTime(parseStrs[1]);
                // 删除最后一个"\n"
                for (int i = 2; i < parseStrs.length; i++) {
                    srtBody.append(parseStrs[i], 0, parseStrs[i].length() - 1);
                }
                srt.setSrtBody(new String(srtBody.toString().getBytes(), StandardCharsets.UTF_8));
                srtMap.put(key, srt);
                key++;
                // 清空，否则影响下一个字幕元素的解析
                sb.delete(0, sb.length());
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private boolean isValid(String[] parseStrs, int cnt) {
        int index = 0;
        for (String parseStr : parseStrs) {
            if (!"".equals(parseStr) && !" ".equals(parseStr)) {
                index++;
            }
        }
        return index >= cnt;
    }


    public TreeMap<Integer, SRT> getSrtMap() {
        return srtMap;
    }
}
